/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Server;

/**
 *
 * @author Mr
 */
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ServerUI extends JFrame implements ActionListener {

    private final DefaultListModel dlm;
    private final JList lst;
    private final JLabel lblMessage;
    private final JButton btnStart, btnStop, btnExit;

    public ServerUI() {
        super("Server RMI");
        lst = new JList(dlm = new DefaultListModel());
        this.add(new JScrollPane(lst), BorderLayout.CENTER);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300, 300);

        JPanel pB = new JPanel(new GridLayout(2, 1));
        JPanel pF = new JPanel();
        pB.add(pF);
        pF.add(btnStart = new JButton("Bắt đầu"));
        pF.add(btnStop = new JButton("Dừng"));
        btnStop.setEnabled(false);
        pF.add(btnExit = new JButton("Thoát"));
        pB.add(lblMessage = new JLabel());

        btnStart.addActionListener(this);
        btnStop.addActionListener(this);
        btnExit.addActionListener(this);

        this.add(pB, BorderLayout.SOUTH);
    }

    public void DislplayInfos(String msg) {
        lblMessage.setText(msg);
    }

    public void addItem(String item) {
        dlm.addElement(item);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            ServerUI ui = new ServerUI();
            java.rmi.registry.LocateRegistry.createRegistry(1099);
            ui.DislplayInfos("Server đã dừng");
            ui.setVisible(true);
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | RemoteException | UnsupportedLookAndFeelException x) {
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object o = e.getSource();
        if (o.equals(btnStart)) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        //java.rmi.registry.LocateRegistry.createRegistry(1099);
                        QLKS_Interface qlks = null;
                        qlks = new QLKS_Implement(ServerUI.this);
                        java.rmi.Naming.rebind("qlks", qlks);
                        lblMessage.setText("Server đang chạy");
                    } catch (MalformedURLException | RemoteException ex) {
                        Logger.getLogger(ServerUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }).start();

            btnStop.setEnabled(true);
            btnStart.setEnabled(false);
        } else if (o.equals(btnStop)) {
            try {
                java.rmi.Naming.unbind("qlks");
                btnStop.setEnabled(false);
                btnStart.setEnabled(true);
                lblMessage.setText("Server đã dừng...");
            } catch (RemoteException | NotBoundException | MalformedURLException ex) {
                Logger.getLogger(ServerUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (o.equals(btnExit)) {
            System.exit(1);
        }
    }
}
